package zzs.generalframework.util.core.money;

/**
 * @title
 * @description
 * @project zzs-util
 * @package zzs.generalframework.util.core.money
 * @author zzs
 * @date 2018年1月30日
 * @version 1.0
 */
public class MoneyUtils
{
	private static MoneyUtils moneyUtils = new MoneyUtils();

	private MoneyUtils()
	{

	}

	public static MoneyUtils getInstance()
	{
		if (null == moneyUtils)
		{
			moneyUtils = new MoneyUtils();
		}
		return moneyUtils;
	}

	/**
	 * 通过大写中文金额获取小写数值金额
	 * 
	 * @param moneyString
	 * @return
	 */
	public String changeMoney(String moneyString)
	{
		String returnString = null;

		if (null != moneyString && !"".equals(moneyString))
		{
			if (-1 != moneyString.indexOf("整"))
			{
				moneyString = moneyString.replace("整", "");
			}

			if (-1 != moneyString.indexOf("零"))
			{
				moneyString = moneyString.replace("零", "");
			}

			long sum = 0;

			String newMoneyString = moneyString;

			if (-1 != newMoneyString.indexOf("亿"))
			{
				String[] splits = newMoneyString.split("亿");
				if (-1 == splits[0].indexOf("壹") || -1 == splits[0].indexOf("贰") || -1 == splits[0].indexOf("叁") || -1 == splits[0].indexOf("肆") || -1 == splits[0].indexOf("伍") || -1 == splits[0].indexOf("陆") || -1 == splits[0].indexOf("柒") || -1 == splits[0].indexOf("捌") || -1 == splits[0].indexOf("玖"))
				{
					splits[0] = "壹" + splits[0];
				}
				sum += getSubMoney(splits[0], "亿");

				returnString = String.valueOf(sum);

				newMoneyString = splits[1];
			}

			if (-1 != newMoneyString.indexOf("万"))
			{
				String[] splits = newMoneyString.split("万");
				if (-1 == splits[0].indexOf("壹") || -1 == splits[0].indexOf("贰") || -1 == splits[0].indexOf("叁") || -1 == splits[0].indexOf("肆") || -1 == splits[0].indexOf("伍") || -1 == splits[0].indexOf("陆") || -1 == splits[0].indexOf("柒") || -1 == splits[0].indexOf("捌") || -1 == splits[0].indexOf("玖"))
				{
					splits[0] = "壹" + splits[0];
				}
				sum += getSubMoney(splits[0], "万");

				returnString = String.valueOf(sum);

				newMoneyString = splits[1];
			}

			if (-1 != newMoneyString.indexOf("元"))
			{

				if (-1 == newMoneyString.indexOf("角"))
				{
					newMoneyString = newMoneyString + "零角";
				}

				if (-1 == newMoneyString.indexOf("分"))
				{
					newMoneyString = newMoneyString + "零分";
				}

				String[] splits = newMoneyString.split("元");

				if (-1 == splits[0].indexOf("壹") || -1 == splits[0].indexOf("贰") || -1 == splits[0].indexOf("叁") || -1 == splits[0].indexOf("肆") || -1 == splits[0].indexOf("伍") || -1 == splits[0].indexOf("陆") || -1 == splits[0].indexOf("柒") || -1 == splits[0].indexOf("捌") || -1 == splits[0].indexOf("玖"))
				{
					splits[0] = "零" + splits[0];
				}

				if (splits.length > 0)
				{
					sum += getSubMoney(splits[0], "元");

					returnString = String.valueOf(sum);

					if (-1 != newMoneyString.indexOf("角") || -1 != newMoneyString.indexOf("分"))
					{
						newMoneyString = splits[1];
					}
					else
					{
						newMoneyString = splits[0];
					}
				}
			}

			if (-1 != newMoneyString.indexOf("分"))
			{
				if (-1 == newMoneyString.indexOf("角"))
				{
					newMoneyString = "零角" + newMoneyString;
				}
			}

			if (-1 != newMoneyString.indexOf("角"))
			{

				String[] spits = newMoneyString.split("角");

				long jiaoMoney = getSubMoney(spits[0], "角");

				returnString = String.valueOf(sum) + "." + String.valueOf(jiaoMoney);

				if (-1 != newMoneyString.indexOf("分"))
				{
					newMoneyString = spits[1];
				}
				else
				{
					newMoneyString = spits[0];
				}
			}

			if (-1 != newMoneyString.indexOf("分"))
			{
				String[] spits = newMoneyString.split("分");

				long fenMoney = getSubMoney(spits[0], "分");

				returnString = returnString + String.valueOf(fenMoney);
			}

		}

		return returnString;
	}

	private long getSubMoney(String money, String unit)
	{
		// 存储子部分的和
		long subSum = 0;

		if (null != money && !"".equals(money) && null != unit && !"".equals(unit))
		{
			char[] moneys = money.toCharArray();

			// 用来记录数字部分
			int rememberInt = 0;

			for (int i = 0; i < moneys.length; i++)
			{
				int increament = increateNum(moneys[i]);

				if (-1 == increament)
				{
					rememberInt = getMoney(moneys[i]);

					if (i == (moneys.length - 1))
					{
						subSum += getMoney(moneys[i]);
					}
				}
				else if (increament > 0)
				{
					// 如果是单位：拾，仟，佰.。乘以对应的进制
					subSum += increament * rememberInt;
				}
				else
				{
					continue;
				}
			}

			return subSum * getUnit(unit);
		}

		return subSum;
	}

	private int increateNum(char c)
	{
		// 根据单位返回要乘的数
		switch (c)
		{
		case '拾':
		{
			return 10;
		}
		case '佰':
		{
			return 100;
		}
		case '仟':
		{
			return 1000;
		}
		case '万':
		{
			return 10000;
		}
		case '零':
		{
			return 0;
		}

		default:
		{
			return -1;
		}
		}
	}

	private int getMoney(char m)
	{
		// 返回大写人民币所对应的数字
		switch (m)
		{
		case '壹':
		{
			return 1;
		}
		case '贰':
		{
			return 2;
		}
		case '叁':
		{
			return 3;
		}
		case '肆':
		{
			return 4;
		}
		case '伍':
		{
			return 5;
		}
		case '陆':
		{
			return 6;
		}
		case '柒':
		{
			return 7;
		}
		case '捌':
		{
			return 8;
		}
		case '玖':
		{
			return 9;
		}
		default:
		{
			return 0;
		}
		}
	}

	private long getUnit(String unit)
	{
		// 根据单位返回零的个数
		if (unit.equals("分"))
		{
			return 1;
		}
		else if (unit.equals("角"))
		{
			return 1;
		}
		else if (unit.equals("元"))
		{
			return 1;
		}
		else if (unit.equals("万"))
		{
			return 10000;
		}
		else if (unit.equals("亿"))
		{
			return 100000000;
		}
		else
		{
			return -1;
		}
	}
}
